package com.iqiyi.pps.epg.core.model.control;

import com.iqiyi.pps.epg.api.model.web.broadcast.TLocationControl;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "location_control")
@Cache(usage = CacheConcurrencyStrategy.NONE)
public class LocationControl {
    private Long id;
    private Long fatherId;
    private Integer location;
    private Integer isDownloadAllowed;
    private Integer isDrm;
    private Integer isShareAllowed;
    private Integer isCooperationAllowed;
    private Integer availableStatus;
    private Integer rejectSearch;
    private Integer rejectRecommend;
    private Integer rejectTop;
    private Integer liveDelayTime;
    private Integer ipSegmentDelayTime;
    private Timestamp addTime = new Timestamp(System.currentTimeMillis());
    private Timestamp updateTime;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "father_id")
    public Long getFatherId() {
        return fatherId;
    }

    public void setFatherId(Long fatherId) {
        this.fatherId = fatherId;
    }

    public Integer getLocation() {
        return location;
    }

    public void setLocation(Integer location) {
        this.location = location;
    }

    @Column(name = "is_download_allowed")
    public Integer getIsDownloadAllowed() {
        return isDownloadAllowed;
    }

    public void setIsDownloadAllowed(Integer isDownloadAllowed) {
        this.isDownloadAllowed = isDownloadAllowed;
    }

    @Column(name = "is_drm")
    public Integer getIsDrm() {
        return isDrm;
    }

    public void setIsDrm(Integer isDrm) {
        this.isDrm = isDrm;
    }

    @Column(name = "is_share_allowed")
    public Integer getIsShareAllowed() {
        return isShareAllowed;
    }

    public void setIsShareAllowed(Integer isShareAllowed) {
        this.isShareAllowed = isShareAllowed;
    }

    @Column(name = "is_cooperation_allowed")
    public Integer getIsCooperationAllowed() {
        return isCooperationAllowed;
    }

    public void setIsCooperationAllowed(Integer isCooperationAllowed) {
        this.isCooperationAllowed = isCooperationAllowed;
    }

    @Column(name = "available_status")
    public Integer getAvailableStatus() {
        return availableStatus;
    }

    public void setAvailableStatus(Integer availableStatus) {
        this.availableStatus = availableStatus;
    }

    @Column(name = "reject_search")
    public Integer getRejectSearch() {
        return rejectSearch;
    }

    public void setRejectSearch(Integer rejectSearch) {
        this.rejectSearch = rejectSearch;
    }

    @Column(name = "reject_recommend")
    public Integer getRejectRecommend() {
        return rejectRecommend;
    }

    public void setRejectRecommend(Integer rejectRecommend) {
        this.rejectRecommend = rejectRecommend;
    }

    @Column(name = "reject_top")
    public Integer getRejectTop() {
        return rejectTop;
    }

    public void setRejectTop(Integer rejectTop) {
        this.rejectTop = rejectTop;
    }

    @Column(name = "live_delay_time")
    public Integer getLiveDelayTime() {
        return liveDelayTime;
    }

    public void setLiveDelayTime(Integer liveDelayTime) {
        this.liveDelayTime = liveDelayTime;
    }

    @Column(name = "ip_segment_delay_time")
    public Integer getIpSegmentDelayTime() {
        return ipSegmentDelayTime;
    }

    public void setIpSegmentDelayTime(Integer ipSegmentDelayTime) {
        this.ipSegmentDelayTime = ipSegmentDelayTime;
    }

    @Column(name = "add_time", updatable = false)
    public Timestamp getAddTime() {
        return addTime;
    }

    public void setAddTime(Timestamp addTime) {
        this.addTime = addTime;
    }

    @Column(name = "update_time", insertable = false, updatable = false)
    public Timestamp getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }

    public TLocationControl entityToThrift() {
        TLocationControl entity = new TLocationControl();
        entity.setId(this.getId());
        entity.setFatherId(this.getFatherId());
        entity.setLocation(this.getLocation());
        entity.setIsDownloadAllowed(this.getIsDownloadAllowed() == 0 ? false : true);
        entity.setIsDrm(this.getIsDrm() == 0 ? false : true);
        entity.setIsShareAllowed(this.getIsShareAllowed() == 0 ? false : true);
        entity.setIsCooperationAllowed(this.getIsCooperationAllowed() == 0 ? false : true);
        entity.setAvailableStatus(this.getAvailableStatus() == 0 ? false : true);
        entity.setRejectSearch(this.getRejectSearch() == 0 ? false : true);
        entity.setRejectRecommend(this.getRejectRecommend() == 0 ? false : true);
        entity.setRejectTop(this.getRejectTop() == 0 ? false : true);
        entity.setLiveDelayTime(this.getLiveDelayTime());
        entity.setIpSegmentDelayTime(this.getIpSegmentDelayTime());
        if (this.getAddTime() != null) {
            entity.setAddTime(this.getAddTime().toString());
        }
        if (this.getUpdateTime() != null) {
            entity.setUpdateTime(this.getUpdateTime().toString());
        }

        return entity;
    }

    public static LocationControl thriftToEntity(TLocationControl entity) {
        LocationControl model = new LocationControl();
        if (entity.getId() > 0) {
            model.setId(entity.getId());
        }
        model.setFatherId(entity.getFatherId());
        model.setLocation(entity.getLocation());
        model.setIsDownloadAllowed(entity.isIsDownloadAllowed() ? 1 : 0);
        model.setIsDrm(entity.isIsDrm() ? 1 : 0);
        model.setIsShareAllowed(entity.isIsShareAllowed() ? 1 : 0);
        model.setIsCooperationAllowed(entity.isIsShareAllowed() ? 1 : 0);
        model.setAvailableStatus(entity.isAvailableStatus() ? 1 : 0);
        model.setRejectSearch(entity.isRejectSearch() ? 1 : 0);
        model.setRejectRecommend(entity.isRejectRecommend() ? 1 : 0);
        model.setRejectTop(entity.isRejectTop() ? 1 : 0);
        model.setLiveDelayTime(entity.getLiveDelayTime());
        model.setIpSegmentDelayTime(entity.getIpSegmentDelayTime());
        if (StringUtils.isNotEmpty(entity.getAddTime())) {
            model.setAddTime(Timestamp.valueOf(entity.getAddTime()));
        }
        if (StringUtils.isNotEmpty(entity.getUpdateTime())) {
            model.setUpdateTime(Timestamp.valueOf(entity.getUpdateTime()));
        }

        return model;
    }
}
